/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.core.base.query;

import vip.dexian.common.query.Filter;
import vip.dexian.common.query.Order;
import vip.dexian.common.query.Pageable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.ui.ModelMap;

import java.util.List;

/**
 * 分页查询器
 *
 * @author 挺好的 2023年06月02日 9:35
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@EqualsAndHashCode (callSuper = false)
public abstract class BasePageableSearcher extends BaseSearchProperty implements BaseSearcher {

    /**
     * 当前第几页
     */
    private Integer pageNumber;

    /**
     * 每页显示多少条
     */
    private Integer pageSize;

    /**
     * 创建分类条件
     *
     * @return 分页条件
     */
    @Override
    public Pageable query () {

        List <Filter> filters = this.addFilters(this.getBeginDate(), this.getEndDate(), this.getSearchProperty(),
                this.getSearchValue(), this.getSearchPropertyType()
        );

        // 添加排序
        List <Order> orders = this.addOrders(this.getOrderProperty(), this.getOrderDirection());

        Pageable pageable = new Pageable();

        pageable.setFilters(filters);
        pageable.setOrders(orders);
        pageable.setPageNumber(this.getPageNumber());
        pageable.setPageSize(this.getPageSize());

        return pageable;
    }

    /**
     * 设置到model map 中
     *
     * @param modelMap
     *         模型数据
     */
    @Override
    public void addAttribute (ModelMap modelMap) {
        modelMap.addAttribute("form", this);
    }
}
